﻿Public Class frmNewRepairation
    Dim Strsql As String
    Public Primary_code As String
    Dim _mysql As New ClsSQLhelper
#Region "Function"
    Sub Load_Repairstatus()
        Try
            Strsql = "Select repair_status_id,repair_status_name from repair_status"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "repair_status")
            With Me.CboRepairation
                .DataSource = DT_Budget
                .DisplayMember = "repair_status_name"
                .ValueMember = "repair_status_id"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Repairstatus"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Function Validate_data() As Boolean
        Validate_data = False
        Try

            If Me.DtpDateSent.DateTime > Now.Date Then
                MsgBox("ไม่สามารถส่งซ่อมโดยใช้วันที่ในอนาคตได้", MsgBoxStyle.Critical, "คำเตือน")
                Exit Function
            End If
            Strsql = "Select count(*) as cc from repairation where item_id = " & Primary_code
            Strsql = Strsql & "  and repair_date = '"
            Strsql = Strsql & Pn_Framework.MysqlDateTimeFormat(Me.DtpDateSent.DateTime, False) & "' "


            Dim DupplicateID As Integer = _mysql.MySQLExecuteScalar(Strsql)
            Select Case DupplicateID
                Case Is > 0
                    MsgBox("ประวัติการซ่อมบำรุงในวันนี้มีอยู่แล้ว", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                    Exit Function
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Validate_data"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select


            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Sub Insert_Data()
        Try
            Strsql = "Insert into repairation (item_id,repair_status,repair_date,return_date,remark,repair_place)"
            Strsql = Strsql & " values(" & Primary_code & "," & CboRepairation.SelectedValue & ",'" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDateSent.DateTime, False)
            Strsql = Strsql & "','" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDateReceive.DateTime, False) & "','" & Me.MemoRemark.Text & "','" & Me.TxtRepairPlace.Text & "')"



            Dim ExecuteResualt As Integer = _mysql.MySQLExecute(Strsql)
            Select Case ExecuteResualt
                Case Is > 0
                    MsgBox("เพิ่มประวัติการซ่อมบำรุงเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการเพิ่มข้อมูล")
                    DialogResult = Windows.Forms.DialogResult.OK
                    Exit Sub
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Insert_Data"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Insert_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region

    Private Sub frmNewRepairation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DtpDateSent.DateTime = Now.Date
        Me.DtpDateReceive.DateTime = Now.Date
        Me.TxtRepairPlace.Text = "โรงซ่อมบำรุง รพ.วิเชียรบุรี"
        Load_Repairstatus()

    End Sub

    Private Sub GroupControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl1.Paint

    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        If Validate_data() = False Then Exit Sub
        Insert_Data()
        Me.Dispose()
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class